﻿using PerformanceBll.Enum;
using PerformanceBll.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;

namespace PerformanceBll
{
    public class LinuxSSH
    {
        Renci.SshNet.SshClient ssh;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="ip">远程IP</param>
        /// <param name="name">用户名</param>
        /// <param name="pwd">密码</param>
        public LinuxSSH(string ip, string name, string pwd)
        {
            ssh = new Renci.SshNet.SshClient(ip, name, pwd);
        }
        public LinuxSSH()
        {
            ssh = new Renci.SshNet.SshClient("52.83.232.61", "root", "W1N#CfQyc");
        }
        public Dictionary<Enum_MonitoringType, decimal> GetCpuMemary()
        {
            var dictionary = new Dictionary<Enum_MonitoringType, decimal>();
            var command = "top -b -n 1";
            ssh.Connect();
            var cmd = ssh.RunCommand(command);
            using (StringReader sr = new StringReader(cmd.Result))
            {
                string str;
                int lineIndex = 0;
                while ((str = sr.ReadLine()) != null)
                {
                    if (str.Contains("Cpu(s):"))
                    {
                        str = str.Replace("Cpu(s):", "").Replace(",", "").Trim();
                        Regex reg = new Regex(@"\s+");
                        string modified = reg.Replace(str, ",");
                        var list = modified.Split(",");
                        dictionary.Add(Enum_MonitoringType.cpu, 100 - Convert.ToDecimal(list[3].Replace("%id", "").ToString()));
                    }
                    else if (str.Contains("Mem:"))
                    {
                        str = str.Replace("Mem:", "").Replace(",", "").Trim();
                        Regex reg = new Regex(@"\s+");
                        string modified = reg.Replace(str, ",");
                        var list = modified.Split(",");
                        var all = Convert.ToDecimal(list[0].Replace("k", "").ToString()) / 1000 / 1000;
                        var use = Convert.ToDecimal(list[2].Replace("k", "").ToString()) / 1000 / 1000;
                        dictionary.Add(Enum_MonitoringType.mem, (decimal)Math.Round((use / all) * 100, 1));
                        dictionary.Add(Enum_MonitoringType.allmen, (decimal)Math.Round((all), 1));
                    }
                }
            }
            command = "df -h";
            cmd = ssh.RunCommand(command);
            using (StringReader sr = new StringReader(cmd.Result))
            {
                string str;
                int lineIndex = 0;
                while ((str = sr.ReadLine()) != null)
                {
                    if (str.Contains("/dev/xvdp1"))
                    {
                        str = str.Replace("/dev/xvdp1", "").Replace("/data", "").Trim();
                        Regex reg = new Regex(@"\s+");
                        string modified = reg.Replace(str, ",");
                        var list = modified.Split(",");
                        dictionary.Add(Enum_MonitoringType.disk, Convert.ToDecimal(list[3].Replace("%", "").ToString()));
                        dictionary.Add(Enum_MonitoringType.alldisk, Convert.ToDecimal(list[0].Replace("G", "").ToString()));
                    }
                }
            }
            return dictionary;
        }
        public List<Chartmodel> GetCpuMemary2(string name)
        {
            var dictionary = new List<Chartmodel>();
            var command = "top";
            ssh.Connect();
            var index = 0;
            while (index < 120)
            {
                var cmd = ssh.RunCommand(command);
                using (StringReader sr = new StringReader(cmd.Result))
                {
                    string str;
                    int lineIndex = 0;
                    var isxun = true;
                    sr.ReadLine();
                    sr.ReadLine();
                    while ((str = sr.ReadLine()) != null && isxun)
                    {
                        if (str.Contains("Cpu(s):"))
                        {
                            //str = str.Replace("Cpu(s):", "").Replace(",", "").Trim();
                            //Regex reg = new Regex(@"\s+");
                            //string modified = reg.Replace(str, ",");
                            //var list = modified.Split(",");
                            //dictionary.Add(new Chartmodel() { key = DateTime.Now.ToString("HH:mm:ss"), value = Convert.ToDecimal(list[0].Replace("%us", "").ToString()) });
                            LoggerHelper.Log(str, name);
                            isxun = false;
                        }
                    }
                }
                index++;
            }
            return dictionary;
        }
    }
}
